var util = require('../../utils/util.js')
// pages/main/index.js
var imgUrls = require('../../utils/data.js')

// 图片上传 util.js 
var uploadFn = require('../../utils/upload.js')

//获取应用实例
const app = getApp()

Page({
  onShareAppMessage: function (res) {
    console.log('/pages/complete-old/index?from=share&img=' + this.data.shareImgUrl);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
      return {
        title: '我的心里话都在这里',
        path: '/pages/complete-old/index?from=share&img=' + encodeURIComponent(this.data.shareImgUrl.replace('.jpg', '')),
        success: function (res) {
          // 转发成功

        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
    console.log('/pages/complete-old/index?from=share&img=' + this.data.shareImgUrl);
    return {
      title: '我的心里话都在这里',
      path: '/pages/tmpList/index' ,
      success: function (res) {
        // 转发成功
        
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  data: {
    // 图片背景切换
    imgUrl: '../../images/bg2.jpg', // 当前要编辑的图片url
    shareImgUrl : '' , // 转发时带生成的服务器图片 链接
    isShare : false ,
    iLeft: 30 ,
    iTop: 53.16157024793388 ,
    iHeight: 445.00000000000006 ,
    iWidth: 315 ,
    formType : '',
    // app.globalData 全局变量
    prefix: app.globalData.oldPrefix ,
    isPass: true

  },
  check() {
    var that = this
    // /v1/wechat/filterText?text = xxx
    var submitData = util.initData
    submitData.data = {
      app: 'NOTES'
    }
    wx.request({
      //填写自己的鉴权服务器地址
      url: 'https://m.viiart.cn/v1/common/isAudit',
      method: 'POST',
      data: submitData,
      success: function (res) {
        console.log(res)
        var isPass = res.data.data.isPass
        that.setData({
          isPass: true
        })

      }
    })
  },
  onLoad: function (options) {
    //this.check()
    var that = this 
    console.log(options)
    if ( options.from == 'share' ){
      if( options.img ){
        
        var url = app.globalData.simplePrefix + decodeURIComponent(options.img) + '.jpg'
        this.data.shareImgUrl = decodeURIComponent(options.img)
        console.log(url)
        this.setData({
          imgUrl: url,
          isShare: true,
        })
        return 
      }else{
        return 
         wx.navigateTo({
           url: '/pages/tmpList/index',
         }) 
         return 
      }
    }else{
      this.setData({
        isShare: false ,
        iLeft: options.offsetLeft ,
        iTop: options.offsetTop ,
        iHeight: options.tmpHeight ,
        iWidth: options.tmpWidth 
      })

    }

    wx.showToast({
      title: '创建场景...',
      icon :'loading' ,
      mask: true,
      duration: 3000,
    })
    // 从缓存中取出链接
    wx.getStorage({
      key: 'myposter',
      success: function (res) {
        console.log(res)
        that.setData({
          imgUrl: res.data
        })
        console.log(res.data)
        // 获取文件名
        var filePath = res.data
        console.log('filePath' ,filePath)
        // 真机： wxfile 
        // 工具模拟  http
        var fileName = filePath.match(/((http:\/\/|wxfile:\/\/))(.+)/)
        console.log(fileName)
        fileName = fileName[3]
        
        console.log('fileName', fileName)
        // 文件上传cos
        uploadFn(filePath, fileName,function (shareImgUrl) {
          wx.hideToast()
          that.setData({
            shareImgUrl: shareImgUrl, // 转发的服务器图片链接
          })
        })
        
      },
      fail: function () {

      }
    })
    //创建初始化图片
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {

    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },

  onUnload: function () {
    // 页面关闭

  },
  // 保存到本地相册
  saveAsAlbum(){
    // 
    var that = this
    wx.showToast({
      title: '保存中...',
      icon: 'loading',
      duration: 1000
    })
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imgUrl ,
      success : function(){
        
        wx.showModal({
          title: '小提示',
          content: '快分享给小伙伴们一起围观吧～',
          showCancel: false,
        })
      },
      fail : function(){
        wx.showModal({
          title: '小提示' ,
          content: '快分享给小伙伴们一起围观吧～',
          showCancel: false, 
        })
      }
    })
  },
  // 发送给朋友
  sendToFriend(){

  },
  toCreatePage(){
    wx.navigateTo({
      url: '/pages/tmpList/index',
    })  
  }
  
 

})